package com.huizhonglm.diap.ui.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.EdgeEffectCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;

import com.huizhonglm.diap.R;
import com.huizhonglm.diap.ui.BaseActivity;
import com.huizhonglm.diap.ui.tab.TabhostActivity;

import java.lang.reflect.Field;

/**
 * 新手引导
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2014-11-26 13:56
 */
public class GuideActivity extends BaseActivity implements View.OnClickListener,
        ViewPager.OnPageChangeListener {

    private ViewPager mViewPager;

    private RadioButton rBtn_1;
    private RadioButton rBtn_2;
    private RadioButton rBtn_3;
    private RadioButton rBtn_4;
    private RadioButton rBtn_5;
    private Button enterBtn;

    private int[] layouts = {
            R.layout.activity_guide_img_1,
            R.layout.activity_guide_img_2,
            R.layout.activity_guide_img_3,
            R.layout.activity_guide_img_4,
            R.layout.activity_guide_img_5};

    private EdgeEffectCompat leftEdge;// ViewPager左边缘对象
    private EdgeEffectCompat rightEdge;// ViewPager右边缘对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        assignViews();
        initEdgeEffectCompat();
    }

    @Override
    protected void assignViews() {
        mViewPager = (ViewPager) findViewById(R.id.guide_img_vp);
        rBtn_1 = (RadioButton) findViewById(R.id.position_1_rbtn);
        rBtn_2 = (RadioButton) findViewById(R.id.position_2_rbtn);
        rBtn_3 = (RadioButton) findViewById(R.id.position_3_rbtn);
        rBtn_4 = (RadioButton) findViewById(R.id.position_4_rbtn);
        rBtn_5 = (RadioButton) findViewById(R.id.position_5_rbtn);
        enterBtn = (Button) findViewById(R.id.enter_app_btn);

        mViewPager.setAdapter(new GuideAdapter());
        mViewPager.setOnPageChangeListener(this);
        enterBtn.setOnClickListener(this);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if(leftEdge != null && rightEdge != null){
            leftEdge.finish();
            rightEdge.finish();
            leftEdge.setSize(0, 0);
            rightEdge.setSize(0, 0);
        }
    }

    @Override
    public void onPageSelected(int position) {
        switch (position){
            case 0:
                rBtn_1.setChecked(true);
                enterBtn.setVisibility(View.GONE);
                break;
            case 1:
                rBtn_2.setChecked(true);
                enterBtn.setVisibility(View.GONE);
                break;
            case 2:
                rBtn_3.setChecked(true);
                enterBtn.setVisibility(View.GONE);
                break;
            case 3:
                rBtn_4.setChecked(true);
                enterBtn.setVisibility(View.GONE);
                break;
            case 4:
                rBtn_5.setChecked(true);
                enterBtn.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    /**
     * 反射ViewPager左右边缘对象（用来去除滑动到边缘时的渐变色）
     */
    private void initEdgeEffectCompat(){
        try {
            Field leftEdgeField = mViewPager.getClass().getDeclaredField("mLeftEdge");
            Field rightEdgeField = mViewPager.getClass().getDeclaredField("mRightEdge");
            if(leftEdgeField != null && rightEdgeField != null){
                leftEdgeField.setAccessible(true);
                rightEdgeField.setAccessible(true);
                leftEdge = (EdgeEffectCompat) leftEdgeField.get(mViewPager);
                rightEdge = (EdgeEffectCompat) rightEdgeField.get(mViewPager);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.enter_app_btn:
                startActivity(new Intent(GuideActivity.this, TabhostActivity.class));
                finish();
                break;
        }
    }

    private class GuideAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return 5;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public View instantiateItem(ViewGroup container, final int position) {
            View v = getLayoutInflater().inflate(layouts[position], container, false);
            container.addView(v);
            return v;
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }
}
